package com.yocard.yxyd.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yocard.yxyd.R;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.http.model.response.OrderListBean;
import com.yocard.yxyd.utils.CommonUtils;
import com.yocard.yxyd.utils.util.StringUtils;
import com.yocard.yxyd.utils.util.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created on 2019/4/3.
 */
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder> {

    private final int TYPE_TOP = 0x11;
    private final int TYPE_NORMOL = 0x22;
    private final int TYPE_BOTTOM = 0x33;
    private List<OrderListBean.OrderList> list;

    public OrderListAdapter(List<OrderListBean.OrderList> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_order, viewGroup, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        try {

            OrderListBean.OrderList item = list.get(i);
            viewHolder.tvName.setText(item.getName());
            viewHolder.tvType.setText(CommonUtils.getLoanTypeStr(item.getLoan_type()));
            viewHolder.tvTime.setText(TimeUtils.getFriendlyTimeSpanByNow2(item.getCheck_at() * 1000));

            viewHolder.tvAmount.setText(item.getAmount() + "万");
            viewHolder.tvPeriod.setText(CommonUtils.getMonth(item.getLoan_period()));
            String line1 = item.getCity().getName();
            line1 += TextUtils.isEmpty(item.getAge()) ? "" : " · " + item.getAge() + "岁";
            line1 += TextUtils.isEmpty(CommonUtils.getCareerStr(item.getCareer())) ? "" : " · " + CommonUtils.getCareerStr(item.getCareer());
            viewHolder.tvLine1.setText(line1);
            String career = item.getCareer();
            String line2 = "";
            if (career.equals(Constants.CAREE_SHANGBAN) || career.equals(Constants.CAREE_ZIYOU)) {
                line2 = "月收入" + CommonUtils.getFormalInt(item.getCash_income()) + "元";
                if (!TextUtils.isEmpty(CommonUtils.getYear(item.getWork_year()))) {
                    line2 += " · " + CommonUtils.getYear(item.getWork_year());
                }
                if (!TextUtils.isEmpty(CommonUtils.getReceivedType(item.getSalary_received_type()))) {
                    line2 += " · " + CommonUtils.getReceivedType(item.getSalary_received_type());
                }
            } else {
                line2 = "经营流水" + CommonUtils.getFormalForQiye(item.getBusiness_amount());
                if (!TextUtils.isEmpty(CommonUtils.getYear(item.getBusiness_year()))) {
                    line2 += " · " + CommonUtils.getYear(item.getBusiness_year());
                }
            }
            viewHolder.tvLine2.setText(line2);
            viewHolder.tvLine3.setText(getZiliao(item));
            viewHolder.tvLine4.setText(CommonUtils.getProfile(item.getCredit_profile()));
            viewHolder.itemView.setTag(item.getId());
            viewHolder.itemView.setOnClickListener(clickListener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        } else if (position == list.size() - 1) {
            return TYPE_BOTTOM;
        } else {
            return TYPE_NORMOL;
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rl_layout)
        RelativeLayout rlLayout;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_amount)
        TextView tvAmount;
        @BindView(R.id.tv_loan_period)
        TextView tvPeriod;
        @BindView(R.id.tv_line_1)
        TextView tvLine1;
        @BindView(R.id.tv_line_2)
        TextView tvLine2;
        @BindView(R.id.tv_line_3)
        TextView tvLine3;
        @BindView(R.id.tv_line_4)
        TextView tvLine4;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.btn_qiangdan)
        TextView btnQiangdan;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (callback != null) {
                String id = (String) v.getTag();
                callback.onReceiveValue(id);
            }
        }
    };

    private ValueCallback<String> callback;

    public void setOnItemClickListener(ValueCallback<String> callback) {
        this.callback = callback;
    }


    private String getZiliao(OrderListBean.OrderList item) {
        List<String> ziliao = new ArrayList<>();
        if (item.getIs_business_license() == 1) {
            ziliao.add("营业执照");
        }
        if (item.getIs_fund() == 1) {
            ziliao.add(" 公积金");
        }
        if (item.getIs_social_insurance() == 1) {
            ziliao.add(" 社保");
        }
        if (item.getIs_car() == 1) {
            ziliao.add(" 名下有车");
        }
        if (item.getIs_house() == 1) {
            ziliao.add(" 名下有房");
        }
        if (item.getIs_zhima() == 1) {
            ziliao.add(" 芝麻分");
        }
        if (item.getIs_weili() == 1) {
            ziliao.add(" 微粒贷");
        }
        if (item.getIs_bxbd() == 1) {
            ziliao.add(" 保单");
        }

        if (ziliao.size() > 3) {
            ziliao = ziliao.subList(0, 3);
        }


        String str = "";
        for (int i = 0; i < ziliao.size(); i++) {
            String s = ziliao.get(i);
            if (i < ziliao.size() - 1) {
                s += " · ";
            }
            str = str + s;
        }

        if (StringUtils.isEmpty(str)) {
            return "无";
        }

        return str.trim();
    }
}
